package com.atguigu.controller;

import cn.hutool.core.util.IdUtil;
import com.atguigu.cloud.apis.PayFeignApi;
import io.github.resilience4j.bulkhead.annotation.Bulkhead;
import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author BLADE
 * @description:
 * @data 2024/4/188:33
 */
@RestController
public class OrderCircuitController {
    @Resource
    private PayFeignApi payFeignApi;

    @GetMapping(value = "/feign/pay/circuit/{id}")
    @CircuitBreaker(name = "cloud-payment-service",fallbackMethod = "myCircuitFallback")
    public String myCircuitBreaker(@PathVariable("id") Integer id){
        return payFeignApi.myCircuit(id);
    }

    /**
     * myCircuitFallback就是服务降级后的兜底处理方法
     * @param id
     * @param throwable
     * @return
     */
    public String myCircuitFallback(Integer id,Throwable throwable){
        return "myCircuitFallback,系统繁忙，请稍后再试--------"+ IdUtil.simpleUUID();
    }

    /**
     * 舱壁隔离
     * @param id
     * @return
     */
    @GetMapping(value = "/feign/pay/bulkhead/{id}")
    @Bulkhead(name="cloud-payment-service",fallbackMethod = "myBulkheadFallback",type=Bulkhead.Type.SEMAPHORE)
    public String myBulkhead(@PathVariable("id") Integer id){
        return payFeignApi.myBulkhead(id);
    }
    public String myBulkheadFallback(Integer id,Throwable throwable){
        return "myBulkheadFallback,隔板超出最大数量限制，系统繁忙，请稍后再试--------"+ IdUtil.simpleUUID();
    }
}
